package com.riko.comic.core.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.experimental.Accessors;
 
/**
 * @author riko
 * @since 2021/10/29
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@Accessors(chain = true)
public class Result<T> {
 
    private Integer code;
 
    private String message;
 
    private T result;
 
    private Integer userId;
 
    private String token;
 
 
    /**
     * 默认
     *
     * @param result
     * @param resultCode
     */
    private Result(T result, ResultCode resultCode) {
        this.result = result;
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }
 
    private Result(T result, Integer code, String message) {
        this.result = result;
        this.code = code;
        this.message = message;
    }
 
 
    private Result(ResultCode resultCode) {
        this.result = null;
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }
 
 
    private Result(boolean code) {
        if (code) {
            this.result = null;
            this.code = ResultCode.SUCCESS.getCode();
            this.message = ResultCode.SUCCESS.getMessage();
        } else {
            this.result = null;
            this.code = ResultCode.SYSTEM_ERROR.getCode();
            this.message = ResultCode.SYSTEM_ERROR.getMessage();
        }
    }
 
 
    public static <T> Result<T> success(T data) {
        return new Result<T>(data, ResultCode.SUCCESS);
    }
 
    public static <T> Result<T> success(T data, String message) {
        return new Result(data, ResultCode.SUCCESS.getCode(), message);
    }
 
    public static Result success() {
        return new Result<>(true);
    }
 
 
 
    public static Result error() {
        return new Result(ResultCode.SYSTEM_ERROR);
    }
 
 
    public static <T> Result<T> error(String errorMessage, Integer errorCode) {
        return new Result<T>(true).setMessage(errorMessage).setCode(errorCode);
    }
 
 
    public static <T> Result<T> error(ResultCode resultCode) {
        return new Result<T>(null, resultCode);
    }
 
 
}